<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BDataObj
 *
 * @author I062261
 */
abstract class BEntity extends BComponent implements BIDataObj, BIResourceEnable {

    const Name_DisplayName = 'displayName';
    const Not_Specify_Value = 'not specify';
    const String_Any = 'any';
    const String_None = 'none';
    const Int_Any = 0;

    //put your code here
    public $id = null;
    public $status;
    protected $saveItems = array();
    protected $loadList = array();
    protected $findContext = array();
    protected $repository = null;

    public function __construct($item = NULL, $context = NULL) {
        // self::initBeforeLoad();
        if ($item != NULL || $context != NULL)
            $this->load($item, $context);
    }

    //set data as true can reload entity
    public function load($data = NULL, $context = NULL) {

        if ($data instanceof BRow)
            return $this->getRepository()->loadEntity($data);

        /*
          $rowContext = $this->getRepository()->populateAttributeList($data);

          if ($rowContext != NULL) {
          return $this->getRepository()->loadEntity($rowContext);
          }
         * 
         */



        $reload = FALSE;

        //if id > 0, means load already, no need to load again
        if ($this->id != NULL && $this->id > 0) {
            //$this->id = $data;
            $reload = FALSE;
        }

        if ($data != NULL && is_numeric($data) && $data > 0) {
            $this->id = $data;
            $reload = TRUE;
        }

        //reload entity if data as true,force reload
        if ($data === TRUE)
            $reload = TRUE;

        if ($reload === TRUE && $this->id != NULL) {
            $repository = BRepositoryFactory::createRepository2();
            return $repository->loadEntity($this);
        }
        //}
    }

    public function save($context = null) {

        $this->saveItems = array();

        if ($context != NULL)
            $this->addSaveItems($context);

        $repository = BRepositoryFactory::createRepository2();

        return $repository->saveEntity($this);
    }

    public function getId($data = null) {
        return $this->id;
    }

    public function getName($data = NULL) {

        if ((isset($data['type']) && strtolower($data['type']) == self::Name_DisplayName) || $data == self::Name_DisplayName) {
            if (isset($this->data['displayName']) && $this->data[
                    'displayName'] != NULL)
                return $this->data['displayName'];
        }

        if (property_exists(get_class($this), 'name'))
            return $this->name;

        return get_class($this);
    }

    public function getResource($data = null) {
        return BResource::find(array('original_resource_id' => $this->getId(), 'original_resource_class' => get_class($this)));
    }

    //delete item just set its status as deleted
    public static function delete($context) {

        $entityClass = get_called_class();
        $entity = new $entityClass();

        if ($entity instanceof BEntity) {
            $modelClass = $entity->getModelClass();
            if ($modelClass == NULL)
                $modelClass = $entity->getRepository()->getModelClass();

            if (class_exists($modelClass)) {
                if (is_numeric($context)) {
                    $entityId = $context;
                }
                if ($context instanceof BEntity) {
                    $entityId = $context->getId();
                }
                $r = $modelClass::model()->findByPk($entityId);
                $r->status = BStatus::Status_Deleted_Id;
                return $r->save();
            }
        }

        //$context = BRepository::getContext($context);
        //$repository = BRepositoryFactory::createRepository();
        //$repository->deletEntity(get_called_class(), $context);
    }

    public static function find($context) {

        $entities = self::findAll($context);

        if ($entities != NULL)
            return reset($entities);
    }

    public static function findById($id, $data = NULL) {
        if (is_numeric($id) && $id > 0) {
            return self::findByPk($id);
        }
        if (is_string($id) && $id != NULL) {
            $class = get_called_class();
            $entity = new $class();
            $row = $entity->getRepository()->getRow(array('myId' => $id));
            if ($row instanceof BRow && $row->getId() != NULL) {
                $entity->load($row);
                return $entity;
            }
        }

        return FALSE;
    }

    public static function findAll($context = NULL) {

        if (!$context instanceof BEntity) {
            $class = get_called_class();
            $entity = new $class();
            $entity->addFindCondition($context);
        } else {
            $entity = $context;
        }

        return BRepositoryFactory::createRepository2()->findAllEntities($entity);
        /*
          $context = BRepository::getContext($context);
          $entity = get_called_class();

          if (isset($this))
          $entity = $this;

          $repository = BRepositoryFactory::createRepository();

          return $repository->findAllEntity($entity, $context);
         * 
         */
    }

    public function getProperties($data = NULL) {

        if ($this->data != NULL)
            return $this->data;

        return FALSE;
    }

    public function getPropertyList($data = NULL) {
        $properties = get_object_vars($this);
        return $properties;
    }

    /**
     * 
     * @param type $name
     * @param type $data
     * @return BAttribute
     */
    public function getAttribute($name, $data = array()) {
        $attributeClass = 'BAttribute';
        $entity = $this;
        if ($this instanceof BAttribute)
            $attributeClass = get_class($this);

        if (is_array($data)) {
            $other = NULL;
            if (isset($data['attributeClass'])) {
                $attributeClass = $data['attributeClass'];
                unset($data['attributeClass']);
            }
            if (isset($data['resourceId'])) {
                $entityClass = get_class($entity);
                $id = $data['resourceId'];
                unset($data['resourceId']);
                $other = $entityClass::findById($id);
            }
            if (isset($data['resource'])) {
                $resource = $data['resource'];
                unset($data['resource']);
                if (is_string($resource)) {
                    $entityClass = get_class($entity);
                    $identity = $resource;
                    $other = $entityClass::findbyId($identity);
                }
                if ($resource instanceof BEntity) {
                    $other = $resource;
                }
            }
            if ($other instanceof BEntity) {
                $myClass = get_class($this);
                $myId = $this->getId();

                $allowMyAccess = $other->getAttribute('allowAccess.' . $myClass . '_' . $myId . "_" . $name)->getValue();
                if ($allowMyAccess) {
                    $entity = $other;
                }
            }
        }

        if ($entity->id != NULL && $entity->id > 0) {
            $attributeContext = new $attributeClass();
            $attributeContext->ownerId = $entity->getId();
            if ($entity instanceof $attributeClass) {
                $attributeContext->ownerId = $entity->ownerId;
            }
            $attributeContext->ownerType = get_class($entity);
            if ($entity instanceof $attributeClass) {
                $attributeContext->ownerType = $entity->ownerType;
            }
            $attributeContext->name = $name;
             if (is_array($data) && $data != NULL) {
                foreach ($data as $name => $value) {
                    if (property_exists($attributeClass, $name)) {
                        $attributeContext->$name = $value;
                    }
                }
            }
            if ($entity instanceof $attributeClass) {
                $attribute = $entity->getChild($attributeContext);
            } else {
                $attribute = $attributeClass::find($attributeContext);
            }
            if ($attribute instanceof $attributeClass) {
                if ($attribute->valueType == BAttribute::Value_Type_None_String) {
                    if (is_string($attribute->data)) {
                        $data = unserialize($attribute->data);
                        if ($data != NULL) {
                            $attribute->value = $data;
                        }
                    }
                }

                return $attribute;
            }
        }
        return FALSE;
    }

    public function getSheet($treeItem = null, $tableName = NULL) {

        if (is_string($treeItem)) {
            if (class_exists($treeItem)) {
                $treeItem = new $treeItem();
            }
        }

        if (!$treeItem instanceof BTreeItem) {
            $treeItem = new BMeta();
        }

        $treeTable = new BTreeTable($treeItem, $this, $tableName);
        return $treeTable;
    }

    /**
     * this function use to find all attributes for entity
     * @param type Array of Attribute
     */
    public function getAttributes($data = array()) {
        $attributeClass = 'BAttribute';
        if ($this instanceof BAttribute)
            $attributeClass = get_class($this);
        if (isset($data['attributeClass']))
            $attributeClass = $data['attributeClass'];
        $attributes = array();
        if ($this->id != NULL && $this->id > 0) {
            $attributeContext = new $attributeClass();
            $attributeContext->ownerId = $this->getId();
            $attributeContext->ownerType = get_class($this);
            //$attributeContext->name = $data;
//            $attributeContext->description = get_class($this);
            //$attributeContext->valueType = $valueType;
            $attributes = $attributeClass::findAll($attributeContext);
        }

        //below code refine to provide correct value
        foreach ($attributes as $attribute) {
            if ($attribute instanceof $attributeClass) {
                if ($attribute->valueType == BAttribute::Value_Type_None_String) {
                    if (is_string($attribute->data))
                        $attribute->value = unserialize($attribute->data);
                    else if ($attribute->data != NULL)
                        $attribute->value = unserialize($attribute->data);
                }
            }
        }
        return $attributes;
    }

    public function addAttribute($name, $value, $data = array()) {
        $attributeClass = 'BAttribute';
        if ($this instanceof BAttribute)
            $attributeClass = get_class($this);
        if (isset($data['attributeClass']))
            $attributeClass = $data['attributeClass'];
        if ($this->id != NULL && $this->id > 0) {
            $attributeContext = new $attributeClass();
            $attributeContext->id = NULL;
            $attributeContext->ownerId = $this->getId();
            $attributeContext->ownerType = get_class($this);
            $attributeContext->name = $name;
            if ($this instanceof $attributeClass) {
                $attributeContext->ownerId = $this->ownerId;
                $attributeContext->ownerType = $this->ownerType;
                //$attributeContext->parentId = $this->getId();
                $attribute = $this->getChild($attributeContext);
            } else {
                $attribute = $attributeClass::getRoot()->getChild($attributeContext);
            }
            if (!$attribute instanceof $attributeClass)
                $attribute = $attributeContext;
            if (is_string($value) || is_numeric($value)) {
                $attribute->valueType = BAttribute::Value_Type_String;
                $attribute->value = $value;
            } else {
                $attribute->valueType = BAttribute::Value_Type_None_String;
                $attribute->data = serialize($value);
                //$attribute->value = $attribute->data;
            }
            $attribute->description = get_class($this) . "_attribute_" . $name;
            if ($attribute->getId() != NULL && $attribute->parentId != NULL)
                $attribute->save();
            else {
                //$attributeContext->value = NULL;
                //$attributeContext->name = NULL;
                if ($this instanceof $attributeClass)
                    $attribute = $this->addChild($attribute);
                else
                    $attribute = $attributeClass::getRoot()->addChild($attribute);
            }

            return $attribute;
        }
        return FALSE;
    }

    /**
     * this function add allow access for other resource
     * @param type $name
     * @param BEntity $resource, can be id, name or entity
     * @return type
     */
    public function addAttributeAllowAcess($name, $resource) {
        $entityClass = get_class($this);
        if (is_string($resource) || is_numeric($resource))
            $other = $entityClass::findById($resource);

        if ($resource instanceof BEntity)
            $other = $resource;

        if ($other instanceof BEntity && $other->getId() != NULL) {
            if (get_class($this) == get_class($other) && $this->getId() == $other->getId())
                return;
            else
                $this->addAttribute('allowAccess.' . get_class($other) . '_' . $other->getId() . '_' . $name, TRUE);
        }
    }

    public function removeAttribute($name, $data = array()) {
        $attributeClass = 'BAttribute';
        if ($this instanceof BAttribute)
            $attributeClass = get_class($this);
        if (isset($data['attributeClass']))
            $attributeClass = $data['attributeClass'];
        if ($this->id != NULL && $this->id > 0) {
            $attributeContext = new $attributeClass();
            $attributeContext->ownerId = $this->getId();
            $attributeContext->ownerType = get_class($this);
            $attributeContext->name = $name;
            $attribute = $attributeClass::find($attributeContext);
            if ($attribute instanceof $attributeClass)
                $attributeClass::delete($attribute);
        }
        return FALSE;
    }

    public function removeAttributeAllowAccess($name, $resouce) {
        $entityClass = get_class($this);
        if (is_string($resource) || is_numeric($resource))
            $other = $entityClass::findById($resource);

        if ($resource instanceof BEntity)
            $other = $resource;

        if ($other instanceof BEntity && $other->getId() != NULL) {
            if (get_class($this) == get_class($other) && $this->getId() == $other->getId())
                return;
            else
                $this->removeAttribute('allowAccess.' . get_class($other) . '_' . $other->getId() . '_' . $name, TRUE);
        }
    }

    public function getPropertyToColumnMapList($data = NULL) {

        $mapList = array();
        $class = get_class($this);

        if (property_exists($class, 'id'))
            $mapList['id'] = '_id';

        if (property_exists($class, 'parentId'))
            $mapList['parentId'] = 'parent_id';

        if (property_exists($class, 'name'))
            $mapList['name'] = 'name';

        if (property_exists($class, 'description'))
            $mapList['description'] = 'description';

        if (property_exists($class, 'spaceId'))
            $mapList['spaceId'] = 'space_id';

        if (property_exists($class, 'appId'))
            $mapList['appId'] = 'app_id';

        if (property_exists($class, 'status'))
            $mapList['status'] = 'status';

        if (property_exists($class, 'value'))
            $mapList['value'] = 'value';

        if (property_exists($class, 'className'))
            $mapList['className'] = 'classname';

        if (property_exists($class, 'relationshipId'))
            $mapList['relationshipId'] = 'relationship_id';

        if (property_exists($class, 'groupClass'))
            $mapList['groupClass'] = 'group_class';

        if (property_exists($class, 'memberClass'))
            $mapList['memberClass'] = 'member_class';

        if (property_exists($class, 'groupId'))
            $mapList['groupId'] = 'group_id';

        if (property_exists($class, 'memberId'))
            $mapList['memberId'] = 'member_id';

        if (property_exists($class, 'parentId'))
            $mapList['parentId'] = 'parent_id';

        if (property_exists($class, 'childId'))
            $mapList['childId'] = 'child_id';



        if (property_exists($class, 'userId'))
            $mapList['userId'] = 'user_id';

        if (property_exists($class, 'password'))
            $mapList['password'] = 'password';

        if (property_exists($class, 'userName'))
            $mapList['userName'] = 'username';

        if (property_exists($class, 'birthday'))
            $mapList['birthday'] = 'birthday';

        if (property_exists($class, 'salt'))
            $mapList['salt'] = 'salt';

        if (property_exists($class, 'email'))
            $mapList['email'] = 'email';

        if (property_exists($class, 'resourceClass'))
            $mapList['resourceClass'] = 'resource_class';

        if (property_exists($class, 'fullName'))
            $mapList['fullName'] = 'fullname';

        if (property_exists($class, 'profile'))
            $mapList['profile'] = 'profile';
        
        if (property_exists($class, 'myid'))
            $mapList['myid'] = 'myid';

        return $mapList;
    }

    public function getModelClass($data = NULL) {
        $class = get_class($this);
        $pos = strpos($class, "BS");
        if ($pos === 0 && !(preg_match("/^BSpace/", $class) || preg_match("/^BStatus/", $class))) {
            $mClass = preg_replace("/^BS/", 'BSM', $class); //for bookingSport model!
        } else
            $mClass = preg_replace("/^B/", 'BM', $class);
        if ($this instanceof BSpace)
            $mClass = 'BMSpace';
        if ($this instanceof BSpaceGroup)
            $mClass = 'BMSpaceGroup';
        if ($this instanceof BApp)
            $mClass = 'BMApp';
        if ($this instanceof BOperation)
            $mClass = 'BMOperation';
        if ($this instanceof BOperationFlow)
            $mClass = 'BMOperationflow';
        return $mClass;
    }

    public function addSaveItems($data = NULL) {

        $saveItems = $this->getSaveItems();

        $class = get_class($this);
        if ($data instanceof $class) {
            $items = array();
            $propertyList = $data->getPropertyList();
            foreach ($propertyList as $key => $value) {
                if ($value != NULL)
                    $items[$key] = $value;
            }
            $data = $items;
        }

        if (is_array($data)) {
            $mapList = $this->getPropertyToColumnMapList();
            foreach ($mapList as $property => $col) {
                if (isset($data[$property]) && $data[$property] != NULL)
                    $saveItems[$col] = $data[$property];
            }
        }

        $this->saveItems = $saveItems;
    }

    public function getSaveItems() {
        //if ($this->saveItems == NULL) {
        $mapList = $this->getPropertyToColumnMapList();

        foreach ($mapList as $property => $col) {
            if (property_exists(get_class($this), $property) && $this->$property != NULL)
                $this->saveItems[$col] = $this->$property;
        }
        //}
        return $this->saveItems;
    }

    public function getLoadList($data = NULL) {
        $mapList = $this->getPropertyToColumnMapList();
        return $mapList;
    }

    public function getFindContext($data = NULL) {

        if ($this->findContext == NULL) {

            $condition = '';
            $params = array();

            if (!$this instanceof BStatus) {
                $statusId = BStatus::Status_Deleted_Id;
                $condition = 'status <> :status';
                $params = array(':status' => $statusId);
            }

            $mapList = $this->getPropertyToColumnMapList();

            $class = get_class($this);
            if ($this instanceof BApp)
                $defaultEntityContext = new $class(BApp::Not_Load_By_ClassName);
            else
                $defaultEntityContext = new $class();

            foreach ($mapList as $property => $col) {
                if (property_exists(get_class($this), $property)) {
                    $value = $this->$property;
                    if ((is_numeric($value) && $value == self::Int_Any) || (is_string($value) && $value == self::String_Any))
                        continue;
                    if ($value == NULL || $value == $defaultEntityContext->$property)
                        continue;

                    if (strlen($condition) != 0)
                        $condition.=' and ';
                    $condition.= $col . ' = :' . $property;
                    $params[":" . $property] = $this->$property;
                }
            }
            $this->findContext = array('condition' => $condition, 'params' => $params);
        }

        return $this->findContext;
    }

    public function addFindCondition($data) {
        $findContext = $this->getFindContext();
        $mapList = $this->getPropertyToColumnMapList();
        if (is_array($data) && $data != NULL) {
            foreach ($data as $property => $value) {
                if ($value != NULL && isset($mapList[$property])) {
                    $col = $mapList[$property];
                    $findContext['condition'].= ' and ' . $col . '= :' . $property;
                    $findContext['params'][":" . $property] = $value;
                }
            }
        }
        $this->findContext = $findContext;
    }

    /**
     * use this function to add special findContext without populate findContext
     * from properties of entity
     * @param array $findContext, format: array('condition'=>...,'params'=>...)
     * 
     */
    public function setFindContext($findContext) {
        $this->findContext = $findContext;
    }

    public static function getEntityContext($data, $entityContext = NULL) {
        //$class = get_called_class();

        if ($entityContext != NULL) {
            $class = get_class($entityContext);
        } else {
            $class = get_called_class();
            if (class_exists($class) && $class != 'BEntity')
                $entityContext = new $class();
        }

        if ($data instanceof $class)
            $entityContext = $data;
        if (is_string($data) && $data != NULL) {
            if (property_exists($class, 'name'))
                $entityContext->name = $data;
        }

        if (is_array($data) && $data != NULL) {
            foreach ($data as $property => $value)
                if (property_exists($class, $property) && $value != NULL)
                    $entityContext->$property = $value;
        }

        return $entityContext;
    }

    public function loadData($loadItems = array()) {
        foreach ($loadItems as $property => $value) {
            if (property_exists(get_class($this), $property)) {
                $this->$property = $value;
            }
        }
    }

    /**
     * entityContext is condition for filter entity
     * @param type $entity
     * @param type $entityContext
     * @return Boolean true means meet rules for context
     */
    public static function filterEntityByContext($entity, $entityContext, $filterList = array()) {
        $properties = $filterList;

        if ($entityContext != NULL) {
            $entityContext = self::getEntityContext($entityContext);
            ///*
            if (is_array($entityContext)) {
                $properties = $entityContext;
            }
            //*/
            if ($entityContext instanceof BEntity) {
                //$properties = $filterList;
                if ($properties == NULL)
                    $properties = get_object_vars($entityContext);
            }

            if ($entity instanceof BEntity) {
                $class = get_class($entity);
                $defaultEntityContext = new $class();
                foreach ($properties as $property => $value) {
                    if (is_numeric($value) && $value == self::Int_Any || (is_string($value) && $value == self::String_Any))
                        continue;
                    if ($value == NULL || $value == $defaultEntityContext->$property)
                        continue;
                    if ($entity->$property == NULL && $value != NULL)
                        return FALSE;
                    if ($value != NULL && $entity->$property != NULL && $entity->$property != $value)
                        return FALSE;
                }
                return TRUE;
            }
        }
    }

    public static function findByPk($id) {
        $class = get_called_class();

        return new $class($id);
    }

    /**
     * 
     * @param type $data
     * @param type $reload
     * @return BRepository3
     */
    public function getRepository($data = NULL, $reload = FALSE) {
        if ($this->repository == NULL || $reload) {
            $this->repository = new BRepository3($this);
            $this->repository->addPropertyToColumnMap('id', '_id');
            $this->repository->addPropertyToColumnMap('name', 'name');
            $this->repository->addPropertyToColumnMap('description', 'description');
            $this->repository->addPropertyToColumnMap('spaceId', 'space_id');
            $this->repository->addPropertyToColumnMap('appId', 'app_id');
            $this->repository->addPropertyToColumnMap('parentId', 'parent_id');
            $this->repository->addPropertyToColumnMap('memberId', 'member_id');
            $this->repository->addPropertyToColumnMap('groupId', 'group_id');
            $this->repository->addPropertyToColumnMap('status', 'status');
            $this->repository->addPropertyToColumnMap('value', 'value');
            $this->repository->addPropertyToColumnMap('className', 'classname');
            $this->repository->addPropertyToColumnMap('groupClass', 'group_class');
            $this->repository->addPropertyToColumnMap('memberClass', 'member_class');
            $this->repository->addPropertyToColumnMap('value', 'value');
            //$this->repository->addPropertyToColumnMap('data', 'data');
            $this->repository->addPropertyToColumnMap('userName', 'username');
            $this->repository->addPropertyToColumnMap('userId', 'user_id');
            $this->repository->addPropertyToColumnMap('password', 'password');
            $this->repository->addPropertyToColumnMap('birthday', 'birthday');
            $this->repository->addPropertyToColumnMap('salt', 'salt');
            $this->repository->addPropertyToColumnMap('email', 'email');
            $this->repository->addPropertyToColumnMap('resourceClass', 'resource_class');
            $this->repository->addPropertyToColumnMap('resourceId', 'resource_id');
            $this->repository->addPropertyToColumnMap('resourceType', 'resource_type');
            $this->repository->addPropertyToColumnMap('type', 'type');
            $this->repository->addPropertyToColumnMap('childId', 'child_id');
            $this->repository->addPropertyToColumnMap('memberClass', 'member_class');
            $this->repository->addPropertyToColumnMap('relationshipId', 'relationship_id');
            $this->repository->addPropertyToColumnMap('fullName', 'fullname');
            $this->repository->addPropertyToColumnMap('profile', 'profile');
            $this->repository->addPropertyToColumnMap('myid', 'myid');
        }
        return $this->repository;
    }

    /**
     * 
     * @param BEntity $member, member should be obj
     * @param BRepositoryContext $data
     * @return boolean
     */
    public function addMember($member, $member2 = NULL, $context = NULL) {

        //parent::addMember($member, $context);
        if ($member instanceof BEntity) {
            $memberRow = $member->getRepository()->getRow();
            $member2Row = NULL;
            if ($member2 instanceof BEntity)
                $member2Row = $member2->getRepository()->getRow();
            if (is_array($context) && $context != NULL)
                $context = $member->getRepository()->propertyListToAttributeList($context);
            $this->getRepository()->getRow()->addMember($memberRow, $member2Row, $context);
        }
    }

    /**
     * get member for current group obj, if set recursive, get all children member
     * @param type $context
     * @param type $recursive
     * @return boolean
     */
    public function getMembers($memberContext = null, $recursive = FALSE) {
        if ($memberContext instanceof BEntity) {
            $memberRowContext = $memberContext->getRepository()->getRow();
            $memberRowList = $this->getRepository()->getRow()->getMembers($memberRowContext);
            $members = array();
            foreach ($memberRowList as $row) {
                $member = clone $memberContext;
                $member->load($row);
                $members[$member->getId()] = $member;
            }
            return $members;
        }
        if (is_array($memberContext)) {
            $member1 = isset($memberContext['member1']) ? $memberContext['member1'] : NULL;
            $member2 = isset($memberContext['member1']) ? $memberContext['member1'] : NULL;
            $returnIndex = isset($memberContext['returnIndex']) ? $memberContext['returnIndex'] : NULL;
            $memberRowContext = array();
            if ($member1 instanceof BEntity)
                $memberRowContext['memberRow'] = $member1->getRepository()->getRow();
            if ($member2 instanceof BEntity)
                $memberRowContext['member2Row'] = $member2->getRepository()->getRow();
            if ($returnIndex != NULL)
                $memberRowContext['returnIndex'] = $returnIndex;
            $memberRowList = $this->getRepository()->getRow()->getMembers(array('memberRow' => $memberRowContext));
            $members = array();
            foreach ($memberRowList as $row) {
                $member = clone $memberContext;
                $member->load($row);
                $members[$member->getId()] = $member;
            }
            return $members;
        }
    }

    public function getMember($memberContext = NULL, $recursive = FALSE) {

        $members = $this->getMembers($memberContext, $recursive);

        if (is_array($members) && $members != NULL)
            return reset($members);

        return FALSE;
    }

}
